package com.nikanbujian.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nikanbujian.reggie.common.R;
import com.nikanbujian.reggie.entity.ShoppingCart;
import com.nikanbujian.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService iShoppingCartService;

//    @GetMapping("/list")
//    public R<String> list(){
//        return R.success("1");
//    }
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request){
        Long user = (Long)request.getSession().getAttribute("user");
        shoppingCart.setUserId(user);
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = iShoppingCartService.getOne(queryWrapper);
        if (one!=null){
            Integer number = one.getNumber();
            one.setNumber(number+1);
            iShoppingCartService.updateById(one);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            iShoppingCartService.save(shoppingCart);

            one=shoppingCart;
        }
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpServletRequest request){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long user =(Long) request.getSession().getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,user);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = iShoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart ,HttpServletRequest request){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long user  = (Long) request.getSession().getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,user);
        if (shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = iShoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        one.setNumber(number-1);
        iShoppingCartService.updateById(one);
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> clean(HttpServletRequest request){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        Long user = (Long) request.getSession().getAttribute("user");
        queryWrapper.eq(ShoppingCart::getUserId,user);
        iShoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }
}
